<template>
  <el-select v-model="formData" clearable @keydown.enter.native="enter" v-bind="$attrs" :placeholder="comPlaceholder">
    <el-option
      v-for="item in comOptions"
      :key="item.value"
      :label="item[comOptionsConfig.label]"
      :value="item[comOptionsConfig.value]">
    </el-option>
  </el-select>
</template>

<script>
export default {
  props: {
    value: {
      type: [String, Number],
      default: "",
    },
  },
  name: "yyInput",
  computed: {
    formData: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      },
    },
    comPlaceholder () {
      return this.$attrs.placeholder || '';
    },
    comOptions () {
      return this.$attrs.options || this.options;
    },
    comOptionsConfig () {
      return Object.assign({ label: 'label', value: 'value'}, this.$attrs.optionsConfig || {});
    }
  },
  data() {
    return {
      options: []
    };
  },
  methods: {
    enter() {
      this.$emit("enter");
    },
    async getOptions () {
      if (this.$attrs.api) {
        const res = await this.$attrs.api();
        this.options = res.data;
      }
    }
  },
  mounted () {
    this.getOptions();
  }
};
</script>

<style>
</style>